<template>
  <div class="meeting-room-list">
    <!-- 筛选条件 -->
    <el-card class="filter-card">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-select v-model="filter.type" placeholder="选择会议室类型">
            <el-option label="教室型" value="classroom"></el-option>
            <el-option label="圆桌型" value="roundtable"></el-option>
          </el-select>
        </el-col>
        <el-col :span="6">
          <el-input-number v-model="filter.minSeats" :min="0" :max="200" placeholder="最小座位数"></el-input-number>
        </el-col>
        <el-col :span="6">
          <el-checkbox-group v-model="filter.equipments">
            <el-checkbox label="projector">投影仪</el-checkbox>
            <el-checkbox label="audio">音响</el-checkbox>
            <el-checkbox label="network">网络</el-checkbox>
          </el-checkbox-group>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="filterRooms">筛选</el-button>
          <el-button @click="resetFilter">重置</el-button>
        </el-col>
      </el-row>
    </el-card>
    
    <!-- 会议室卡片列表 -->
    <el-row :gutter="20" class="room-card-container">
      <el-col :span="6" v-for="room in filteredRooms" :key="room.id">
        <el-card class="room-card" :class="{ 'room-busy': room.status !== 'free' }">
          <div class="room-header">
            <h3>{{ room.name }}</h3>
            <el-tag :type="getStatusType(room.status)">{{ room.status }}</el-tag>
          </div>
          <div class="room-info">
            <p>类型: {{ room.type === 'classroom' ? '教室型' : '圆桌型' }}</p>
            <p>座位数: {{ room.seats }}人</p>
            <p>设备: {{ getEquipmentList(room.equipments) }}</p>
            <p class="room-price">¥{{ room.price }}/小时</p>
          </div>
          <div class="room-actions">
            <el-button 
              v-if="room.status === 'free'" 
              type="primary" 
              @click="goToDetail(room.id)"
            >
              预订
            </el-button>
            <el-button v-else disabled>已占用</el-button>
          </div>
        </el-card>
      </el-col>
    </el-row>
    
    <!-- 分页 -->
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import { useRouter } from 'vue-router';
import { getMeetingRooms } from '@/api/meetingRoom';

const router = useRouter();
const filter = ref({
  type: '',
  minSeats: 0,
  equipments: []
});
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);

// 获取会议室列表
const fetchRooms = async () => {
  const params = {
    page: currentPage.value,
    pageSize: pageSize.value,
    ...filter.value
  };
  const response = await getMeetingRooms(params);
  total.value = response.total;
  return response.rooms;
};

// 计算筛选后的会议室列表
const rooms = ref([]);
const filteredRooms = computed(() => {
  return rooms.value;
});

// 初始化数据
const initData = async () => {
  rooms.value = await fetchRooms();
};

initData();

// 筛选会议室
const filterRooms = async () => {
  currentPage.value = 1;
  rooms.value = await fetchRooms();
};

// 重置筛选条件
const resetFilter = () => {
  filter.value = {
    type: '',
    minSeats: 0,
    equipments: []
  };
  filterRooms();
};

// 前往详情页
const goToDetail = (id) => {
  router.push(`/rooms/${id}`);
};

// 获取状态对应的类型
const getStatusType = (status) => {
  const types = {
    'free': 'success',
    'locked': 'warning',
    'booked': 'primary',
    'using': 'info',
    'maintenance': 'danger'
  };
  return types[status] || '';
};

// 获取设备列表字符串
const getEquipmentList = (equipments) => {
  const equipmentMap = {
    'projector': '投影仪',
    'audio': '音响',
    'network': '网络'
  };
  return equipments.map(equip => equipmentMap[equip]).join('、');
};

// 分页处理
const handleSizeChange = (val) => {
  pageSize.value = val;
  filterRooms();
};

const handleCurrentChange = (val) => {
  currentPage.value = val;
  filterRooms();
};
</script>

<style scoped>
.meeting-room-list {
  padding: 20px;
}

.filter-card {
  margin-bottom: 20px;
}

.room-card-container {
  margin-bottom: 20px;
}

.room-card {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.room-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.room-info {
  flex-grow: 1;
  margin-bottom: 15px;
}

.room-price {
  font-size: 18px;
  font-weight: bold;
  color: #f56c6c;
  margin-top: 10px;
}

.room-actions {
  text-align: right;
}

.room-busy .el-card__header {
  background-color: #f5f7fa;
}
</style>  